<?php

namespace Biz\service\trade;

use Biz\tools\ShopArea as tArea;
use Hhz\AdministrativeRegion\Region;
use Biz\cache\UserAddress as cUserAddress;
use Biz\model\trade\UserAddress as mUserAddress;

class UserAddress
{

    //节选现在收货地区的商品一级类目id
    const RECEIVE_LIMIT_CATEGORY_ID = [
        1,//家具
    ];
    //节选支持配送的地区code
    const RECEIVE_AREA_CODE = [
        310000,//上海
    ];

    /**
     * 获取默认地区城市
     *
     * @param $uid
     * @return string
     */
    public static function getUserDefaultCityCode($uid)
    {
        $cacheData = cUserAddress::getUserDefaultCityd($uid);
        if (is_null($cacheData)) {
            $cityId = mUserAddress::getUserDefaultCityCode($uid);
            cUserAddress::setUserDefaultCityId($uid, $cityId ?: 0);
            return $cityId;
        } else {
            return $cacheData;
        }
    }


    public static function getDefaultAddress($uid)
    {
        return mUserAddress::getDefaultAddress($uid);
    }

    public static function getByAcceptId($acceptId)
    {
        return mUserAddress::getById($acceptId);
    }

    public static function getAllAddressByUid($uid)
    {
        $address = mUserAddress::getAllAddressByUid($uid);
        if (!empty($address)) {
            foreach ($address as &$val) {
                $province = Region::getProvinceCode($val['area_code']);
                $city = Region::getCityCode($val['area_code']);
                $area = $val['area_code'];
                $val['province'] = $province;
                $val['city'] = $city;
                $val['area'] = $area;
                $val['address_id'] = $val['id'];
            }
        }
        return $address;
    }

    public static function deleteAddressById($addressId, $uid)
    {
        if (!mUserAddress::checkUserAddressById($addressId, $uid)) {
            throw new \Exception("没有该用户地址");
        }
        return mUserAddress::deleteAddressById($addressId);
    }

    public static function saveDefaultAddress($data)
    {
        if (!mUserAddress::checkUserAddressById($data['address_id'], $data['uid'])) {
            throw new \Exception("没有该用户地址");
        }
        return mUserAddress::saveDefaultAddress($data);
    }

    /**
     * 编辑保存用户地址
     * todo 6.0版本后不再使用
     *
     * @param $addressId
     * @param $data
     * @return array|mixed
     * @throws \Exception
     */
    public static function createOrEdit($addressId, $data)
    {
        $province = tArea::locationId2Name($data['province']);
        $city = tArea::locationId2Name($data['city']);
        if ($city == '市辖区' || $city == '县') {
            $city = '';
        }
        $area = tArea::locationId2Name($data['area']);
        $data['address'] = $province . $city . $area . $data['info_address'];
        if ($addressId) {
            $num = mUserAddress::getNumOfAddressByUid($data['uid']);
            if ($num <= 1) {
                $data['is_default'] = 1;
            }
            if (!mUserAddress::checkUserAddressById($addressId, $data['uid'])) {
                throw new \Exception("没有该用户地址");
            }
            $result = mUserAddress::saveAddressById($addressId, $data);
        } else {
            $result = mUserAddress::action(function () use ($data) {
                if (!mUserAddress::hasMasterAddressByUid($data['uid'])) {
                    $data['is_default'] = 1;
                }
                $result = mUserAddress::insertAddress($data);
                return $result;
            });
        }
        return $result;
    }

    /**
     * 编辑保存用户地址(新)
     *
     * @param $addressId
     * @param $data
     * @return void
     * @throws \Exception
     */
    public static function saveOrEdit($addressId, $data)
    {
        $data['area_code'] = $data['area_code'] ? trim($data['area_code']) : '';
        $data['address'] = $data['area_code'] ? Region::getAreaDetailByCode($data['area_code']) . $data['info_address'] : '';
        if ($addressId) {
            $num = mUserAddress::getNumOfAddressByUid($data['uid']);
            if ($num <= 1) {
                $data['is_default'] = 1;
            }
            if (!mUserAddress::checkUserAddressById($addressId, $data['uid'])) {
                throw new \Exception("没有该用户地址");
            }
            $result = mUserAddress::saveAddressById($addressId, $data);
        } else {
            $result = mUserAddress::action(function () use ($data) {
                if (!mUserAddress::hasMasterAddressByUid($data['uid'])) {
                    $data['is_default'] = 1;
                }
                return mUserAddress::insertAddress($data);
            });
        }
        return $result;
    }

}